package com.xbongbong.crm.handle.explain.impl;

import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.interfaces.explain.BusinessExplainHandle;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.OperatePageEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.help.ExplainHelp;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/18 17:16
 */
@Service("contractExplainHandleImpl")
@Slf4j
public class ContractExplainHandleImpl implements BusinessExplainHandle {

    @Resource
    private ExplainHelp explainHelp;
    @Resource
    private CompanyConfigModel companyConfigModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }

    @Override
    public List<FieldAttrEntity> newHandleExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> entities = formatExplainDTO.getExplainList();
        String corpid = formatExplainDTO.getCorpid();
        for (FieldAttrEntity fieldAttrEntity : entities) {
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(attr, ContractEnum.PRODUCT.getAttr())) {
                explainHelp.formatSelectProductExplains(fieldAttrEntity, XbbRefTypeEnum.CONTRACT, corpid, OperatePageEnum.NEW.getCode(), false);
                fieldAttrEntity.setSupportScanning(BasicConstant.ONE);
                CompanyConfigEntity priceConfig = companyConfigModel.getByConfigAlias(CompanyConfigEnum.PRICE_MANAGE.getAlias(), corpid);
                if (priceConfig != null && Objects.equals(priceConfig.getConfigValue(), "1")) {
                    SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
                    saasParticularAttributePoJo.setIsOpenPrice(BasicConstant.ONE);
                    fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
                }
            } else if (Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.ADD_PAYMENT.getAttr())) {
                if (!formatExplainDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.PAYMENT_VIEW.getAlias())) {
                    fieldAttrEntity.setVisible(0);
                }
                if (!formatExplainDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.PAYMENT_ADD.getAlias())) {
                    fieldAttrEntity.setEditable(0);
                }
//                fieldAttrEntity.setEditable(0);
            } else if (Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.PRODUCT_TOTAL.getAttr())) {
                fieldAttrEntity.setEditable(0);
            } else if ((Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.ADD_WRITE_OFF_PREPAYMENT.getAttr())
                    || Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.ADD_PAYMENT_SHEET.getAttr()))) {
                if (!formatExplainDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.PAYMENT_SHEET_VIEW.getAlias())) {
                    fieldAttrEntity.setVisible(0);
                }
                if (!formatExplainDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.PAYMENT_SHEET_ADD.getAlias())) {
                    fieldAttrEntity.setEditable(0);
                }
            }
        }
        return entities;
    }
}
